package com.qianfeng.day11.service;

import com.qianfeng.day11.dao.UserDAO;
import com.qianfeng.day11.entity.User;
import com.qianfeng.day11.exception.PasswordIncorrectException;
import com.qianfeng.day11.exception.UnkownUsernameException;
import com.qianfeng.day11.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;

public class UserLoginServiceImpl {

    @Autowired
    private UserDAO userDAO;

    public User login(String username, String password) throws Exception{
        if ("".equals(username) || username == null) {
            throw new UnkownUsernameException();
        }
        //校验用户名或者密码是否正确
        User user = userDAO.findusername(username);
        if (user == null) {
            throw new UnkownUsernameException();
        }
        //将用户传入的密码进行加密
        String md5 = MD5Utils.md5(password, user.getSalt());

        //校验密码
        if (!user.getPassword().equals(md5)) {
            throw new PasswordIncorrectException();
        }

        return user;
    }



}
